<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Api extends WD_Controller {

    public function index(){

        $host = \libraries\waydiao\Wdata::getHost();

        $treeData = \libraries\waydiao\Wdata::getTreeData();

        $data = [
            'host' => $host,
            'treeData' => $treeData,
        ];

        $this->load->view('api/index', $data);
    }

	/**
	 * API
	 */
	public function detail(){

		$uri = isset($_GET["uri"]) ? addslashes($_GET["uri"]) : "";

//		$api = \libraries\waydiao\Wdata::getApi($apiid);
		$api = \libraries\waydiao\Wdata::getApiByUri($uri);
		$host = \libraries\waydiao\Wdata::getHost();

		$data = [
			'host' => $host,
			'api' => $api,
		];

		$this->load->view('api/detail', $data);

	}

	public function asyncDetail(){

		$uri = isset($_GET["uri"]) ? addslashes($_GET["uri"]) : "";
		$api = \libraries\waydiao\Wdata::getApiByUri($uri);

		$data = [];
		if(isset($api["params"]) && is_array($api["params"])) {
			foreach ($api["params"] as $param) {
				$data[$param["name"]] = isset($_POST[$param["name"]]) ? $_POST[$param["name"]] : $param["default"];
			}
		}

		echo "请求参数：";
		pre($data);

		$host = \libraries\waydiao\Wdata::getHost();
		$request_url = ($api["host"]=="oauth") ? $host["oauth"].$api["uri"] : $host["yuxun"].$api["uri"];

		echo "请求地址：".$request_url."<br>";

		$this->load->library('session');
		$Authorization = isset($_SESSION["access_token"]) ? $_SESSION["access_token"] : "";

		echo "header："."<br>";
		$header = empty($Authorization) ? [] : ["Authorization:Bearer ".$Authorization];
		pre($header);

		echo "---------------------------<br>";

		if(strtoupper($api["method"]) == 'POST'){
			if($api["isAuth"]){
				$r = curl_post_header($request_url, $header, json_encode($data));
			}else{
				$r = curl_post_header($request_url, $header, $data);
			}
		}else{
			$r = curl_get_request($request_url, $data, $header);
		}
		
		//file_put_contents('d:\aa.txt', $r);

		echo "文本："."<br>";
		echo $r;
		
		echo "<br><br>";

		if($r2 = json_decode($r)){
			echo "数组："."<br>";
			echo '<pre>';print_r($r2);echo '</pre>';
			
			echo "JSON："."<br>";
			echo indent(json_encode($r2, 256));
		}


		if($uri == '/oauth2/token'){
			if(isset($r2->body->access_token) && !empty($r2->body->access_token)){
				$this->load->library('session');
				$_SESSION["access_token"] = $r2->body->access_token;
			}
		}

	}

	public function raw(){
		$uri = isset($_GET["uri"]) ? addslashes($_GET["uri"]) : "";

		echo $uri."<br>";

		$api = \libraries\waydiao\Wdata::getApiByUri($uri);

		$data = [];
		$Authorization = isset($_SESSION["access_token"]) ? $_SESSION["access_token"] : "";
		if(!empty($Authorization)){
			$data["access_token"] = $Authorization;
		}

		
		if(isset($api["params"]) && is_array($api["params"])) {
			foreach ($api["params"] as $param) {
				$data[$param["name"]] = isset($_POST[$param["name"]]) ? $_POST[$param["name"]] : $param["default"];
				if($temp = json_decode($data[$param["name"]], true)){
					$data[$param["name"]] = $temp;
				}
			}
		}



		echo "请求参数：";
		$raw_str = json_encode($data);
		echo $raw_str."<br>";

		$host = \libraries\waydiao\Wdata::getHost();
		$request_url = ($api["host"]=="oauth") ? $host["oauth"].$api["uri"] : $host["yuxun"].$api["uri"];

		echo "请求地址：".$request_url."<br>";

		if($api["isAuth"]){
			$this->load->library('session');
			$header = ["Authorization:Bearer ".$Authorization];
			echo $Authorization."<br>";

		}else{
			$header = [];
		}

		pre($header);
		echo "---------------------------<br>";


		$r = \libraries\Curl::header_raw($request_url, $header, $raw_str);

		var_dump($r);


		if($r2 = json_decode($r)){
			echo "数组："."<br>";
			echo '<pre>';print_r($r2);echo '</pre>';
			
			echo "JSON："."<br>";
			echo indent(json_encode($r2, 256));
		}
		
		
		
	}


}
